package com.wanwu.channel.dao.config;

import com.google.common.collect.Maps;
import com.wanwu.channel.dao.datasource.DataSourceType;
import com.wanwu.channel.dao.datasource.MultipleRoutingDataSource;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import com.alibaba.druid.spring.boot.autoconfigure.DruidDataSourceBuilder;

import javax.sql.DataSource;
import java.util.Map;

/**
 * druid数据源配置
 *
 * @author xun
 * @version 1.0
 * @date 2019-03-19 20:27
 * @project clear
 */
@Configuration
@MapperScan(value = {"com.wanwu.channel.dao.inter"})
public class MybatisConfig {


    /**
     * master数据源
     *
     * @return DataSource
     */
    @Primary //主数据源
    @Bean(name = "masterDataSource")
    @ConfigurationProperties("spring.datasource.master")
    public DataSource dataSourceMaster() {
        return DruidDataSourceBuilder.create().build();
    }



    /**
     * slave1数据源
     *
     * @return DataSource
     */
    @Bean(name = "offLineDataSource")
    @ConfigurationProperties("spring.datasource.offline")
    public DataSource dataSourceOffLine() {
        return DruidDataSourceBuilder.create().build();
    }

    /**
     * 设置多数据源
     *
     * @return
     */
    @Bean
    public MultipleRoutingDataSource multipleDataSource() {
        DataSource master = this.dataSourceMaster();
        DataSource offLine = this.dataSourceOffLine();
        MultipleRoutingDataSource multipleDataSource = new MultipleRoutingDataSource();
        multipleDataSource.setDefaultTargetDataSource(master);
        Map<Object, Object> targetDataSource = Maps.newHashMap();
        targetDataSource.put(DataSourceType.MASTER.name(), master);
        targetDataSource.put(DataSourceType.OFF_LINE.name(), offLine);
        multipleDataSource.setTargetDataSources(targetDataSource);
        return multipleDataSource;
    }

    @Bean
    public SqlSessionFactoryBean sqlSessionFactory(MultipleRoutingDataSource dataSource) throws Exception {
        SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
        bean.setDataSource(dataSource);
        bean.setConfigLocation(new PathMatchingResourcePatternResolver().getResource("classpath:channel-dao-mybatis.xml"));
        bean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath:mapper/*.xml"));
        return bean;
    }

    @Bean
    public DataSourceTransactionManager transactionManager(MultipleRoutingDataSource dataSource){
        DataSourceTransactionManager bean = new DataSourceTransactionManager();
        bean.setDataSource(dataSource);
        return bean;
    }
}
